import sys, pygame
from random import randint
from time import sleep
from math import sin,cos,tan,asin,acos,atan,atan2,floor
def randcolour():
	return (randint(0,255),randint(0,255),randint(0,255))
def gravitational_force(point1,point2,mass1,mass2):
	global g
	
	deltax = point2[0] - point1[0]
	deltay = point2[1] - point1[1]
	try:
		force_g = (G*mass1*mass2)/((deltax**2+deltay**2)**0.5)
	except:
		force_g = 0	
	theta = atan2(deltay,deltax)
	y = force_g*sin(theta)
	x = force_g*cos(theta)	
	g_vector=[x,y]
	return g_vector
def applyforce(forcevector,particlemass):
	forcevector[0] = forcevector[0] / particlemass
	forcevector[1] = forcevector[1] / particlemass
	return forcevector
class Particle:
	def __init__(self,density,size):
		global speedcap
		self.size = size
		self.mass = density*(size**2)
		self.Ball = pygame.Surface([size,size])
		self.Ball.fill([0,0,0])
		self.Ball.set_colorkey([0,0,0])
		pygame.draw.circle(self.Ball,randcolour(),(size/2,size/2),size/2)
		self.BallRect = self.Ball.get_rect()
		self.BallRect.center = [randint(size,width-size),randint(size,height-size)]
		self.Speed = [randint(1,speedcap[0]),randint(1,speedcap[1])]
	def newcolour(self):
		size = self.size
		self.Ball.fill([0,0,0])
		self.Ball.set_colorkey([0,0,0])
		pygame.draw.circle(self.Ball,randcolour(),(size/2,size/2),size/2)
	def draw(self):
		global screen
		screen.blit(self.Ball,self.BallRect)	
	def move(self):
		
		self.Speed[0] = round(float(self.Speed[0]),4)
		self.Speed[1] = round(float(self.Speed[1]),4)
		
		self.BallRect = self.BallRect.move(self.Speed)
		if self.BallRect.left <= 0 or self.BallRect.right >= width:
			self.Speed[0] *= -1
		if self.BallRect.top <= 0 or self.BallRect.bottom >= height:
			self.Speed[1] *= -1	
		
	def modspeed(self,speed,cons=1):
		global speedcap
		if abs(self.Speed[0]+speed[0]) <= speedcap[0]:
			self.Speed[0] += round(cons*speed[0],4)
			
		if abs(self.Speed[1]+speed[1]) <= speedcap[1]:
			self.Speed[1] += round(cons*speed[1],4)
		self.Speed[1] = round(self.Speed[1],4)	
		self.Speed[0] = round(self.Speed[0],4)	
pygame.init()
size = width, height = 800, 600

screen = pygame.display.set_mode(size)
particles = []
speedcap = [10,10]
numberspawns = 20
collelast = 1
G = 0.025
black = (0,0,0)
screensurface = pygame.Surface([width,height])
screensurface.fill(black)
screensurface.set_colorkey(black)
screenrect = screensurface.get_rect()
screenrect.center = [width/2,height/2]
massconstant = 1.0/10
center = 1
effects = 1
mousemass = 0
maxmousemass = 500000
minmousemass = -500000
permamouse = [width/2,height/2]
for i in range(0,numberspawns):
	b_size = randint(10,20)
	particles.append(Particle(massconstant,b_size))

while 1:
	
	data = pygame.mouse.get_pressed()
	if data[0]:
		if mousemass + 10 <= maxmousemass:
			mousemass += 10
	if data[2]:	
		if mousemass - 10 >= minmousemass:
			mousemass -= 10
	print mousemass	
	screensurface.fill(black)
	screen.fill(black)
	for event in pygame.event.get():
		if event.type == pygame.QUIT: sys.exit()
		if event.type == pygame.MOUSEBUTTONDOWN:
			data = pygame.mouse.get_pressed()
			if data[1]:
				center = 1 - center
				permamouse = pygame.mouse.get_pos()
				
	for i in range(0,len(particles)):
		for j in range(0,len(particles)):
			gravity_force = gravitational_force(particles[i].BallRect.center,
			particles[j].BallRect.center,
			particles[i].mass,
			particles[j].mass)
			actual_force = applyforce(gravity_force,particles[i].mass)
			particles[i].modspeed(actual_force)
			if i != j:
				collide = particles[i].BallRect.colliderect(particles[j].BallRect)
				if collide:
					jforce = [0,0]
					iforce = [0,0]
					jforce[0] = particles[j].Speed[0]
					jforce[1] = particles[j].Speed[1]
					iforce[0] = particles[i].Speed[0]
					iforce[1] = particles[i].Speed[1]
					particles[i].Speed = jforce
					particles[j].Speed = iforce
					particles[i].newcolour()
					particles[j].newcolour()
					
		
		
		if center:
			mouse = pygame.mouse.get_pos()
			
		else:
			mouse = permamouse
		mouse_gravity = gravitational_force(
		particles[i].BallRect.center,
		mouse,
		particles[i].mass,
		mousemass)
		f = applyforce(mouse_gravity,particles[i].mass)
		particles[i].modspeed(f)
		particles[i].move()
		particles[i].draw()
		pcenter = particles[i].BallRect.center
		if effects == 1:
			pygame.draw.aaline(screensurface,randcolour(),pcenter,mouse,100)
	screen.blit(screensurface,screenrect)
	pygame.display.flip()
	sleep(0.02)
	
